<?php
namespace app\http\controller;
use think\Controller;
use think\Db;
use think\facade\Log;
use app\service\swooleService;
use app\service\orderService;
use app\service\apiparkService;
use app\service\apiService;
use app\service\hzcityService;
use printing\printDemo;
class Carapi extends Controller{
    public function applypay($indata){
       
        $couponCarInfo=(new swooleService())->couponCarInfo($indata["parkingNo"],$indata["carPlateNo"]);
        //return $sql2;
       
        $loginfo=(new swooleService())->loginfo($indata["parkingNo"],$indata["clientNo"]);
        
        if(!empty($couponCarInfo)){
            //优惠券支付返回的命令
            $result=(new swooleService())->couponCommand($indata,$loginfo);
            return $result;
        }
        //判断是不是储值车
        $carvalueInfo=(new swooleService())->carvalueInfo($indata["parkingNo"],$indata["carPlateNo"]);
        if(!empty($carvalueInfo)){
            if($carvalueInfo["balance"]>=($indata["amountRec"]/100)){
                $command=(new SwooleService())->carvalueCommand($indata,$carvalueInfo,$loginfo);
                return $command;
            }
        }    
        //判断是不是福利车
        
        //查询是否是福利车
        $carwelfareInfo=(new swooleService())->carwelfareInfo($indata["parkingNo"],$indata["carPlateNo"]);
        if(!empty($carwelfareInfo)){
            //获取福利车订单参数
            $car_welfare_order=(new swooleService())->getCarWelfareOrder($indata,$carwelfareInfo);
            //查询福利车当前已使用的时间
			$time=(new swooleService())->getCarWelfareTime($carwelfareInfo,$car_welfare_order);
			
			if($time<$loginfo["welfare_time"]*60*60){
			    //执行免费操作
			    $command=(new swooleService())->carwelfareCommand($indata,$loginfo,$car_welfare_order);
			    return $command;
			}
			
        }
        
        
        //城市大脑扣款
        if($loginfo["hzcity_status"]=="1"){
            //查询车辆先离场后付费签约信息。
            $departurePayInfo["parkingCode"]=$loginfo["parking_id"];
            
            $departurePayInfo["plateNo"]=$indata["carPlateNo"];
            $departurePayInfo["plateColor"]="1";
            $payres=(new hzcityService())->getDeparturePayInfo($departurePayInfo);
            $payres=json_decode($payres,true);
            if($payres["resultCode"]=="200"){
                if($payres["noSense"]=="1"&&$payres["moneyLimit"]>=$indata["amountDue"]){
                    //直接放行
                    $result=(new swooleService())->hzcityCommand($indata,$loginfo);
                    return $result;
                }
            }
        }elseif($loginfo["hzcity_status"]=="2"){
            //查询该车辆是否开通了无感
            $api = new apiparkService();
            $wgres = $api->agreementQuery($indata["carPlateNo"]);
            //车辆签约无感
            if(($wgres->code=="10000")&&($wgres->agreement_status=="0")){
                $wgres=(new swooleService())->wuganCommand($indata,$loginfo);
                //Log::write($wgres, 'wgres', true);
                return $wgres;
                
            }
        }else{
            $response["bizCode"]="B1010";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="10006";
            $response["respMsg"]="车辆未开通无感支付";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
    }

    //月卡会员操作
    public function carPackage($indata){
        $result=(new swooleService())->carPackage($indata);
        return $result;
    }
    
    public function carout($indata){
        
        $result=(new swooleService())->carOut($indata);
        return $result;
    }
    public function close($fd){
        $res=Db::table("client_login_car")->where("fd",$fd)->find();
        if(!empty($res)){
            $res2=Db::table("client_login_car")->where("fd",$fd)->delete();
            return $res2;
        }else{
            $res2=Db::table("client_login")->where("fd",$fd)->update(["connect_status"=>"OFF"]);
            return $res2;
        }
    }
    public function getFd($indata){
        $info=Db::table("client_login_car")->where("parkingNo",$indata["parkingNo"])->where("clientNo",$indata["clientNo"])->find();
        return $info["fd"];
    }

    //查找预支付下发的客户端
    public function getPrepayClientFd($parkingNo){
        $loginfo=Db::table("client_login")->where("parkingNo",$parkingNo)->where("connect_status","ON")->find();
        
        return $loginfo["fd"];
    }
    //添加月租车下发的客户端
    public function clinetPackageAdd($parkingNo,$fd){
        $info["parkingNo"]=$parkingNo;
        $info["fd"]=$fd;
        $info["ctype"]=2;
        $res=Db::table("client_login_car")->insert($info);
        return $res;

    }
    //查找月租车下发的客户端
    public function getPackageClientFd($parkingNo){
        $loginfo=Db::table("client_login_car")->where("parkingNo",$parkingNo)->where("ctype","2")->find();
        return $loginfo["fd"];
    }
    public function getClientFd($clientNo,$parkingNo){
        $loginfo=Db::table("client_login")->where("clientNo",$clientNo)->where("parkingNo",$parkingNo)->find();
        
        return $loginfo["fd"];
    }
    
    public function prePay($indata,$fd){
        Db::table("client_login_car")->where("fd",$fd)->delete();
        $parkingNo=$indata["parkingNo"];//停车场
        $clientNo=$indata["clientNo"];//客户端
        $carPlateNo=$indata["carPlateNo"];//出口相机id
        $reqNo=date("YmdHis")."-".rand(100000,999999);
        
        $user["parkingNo"]=$parkingNo;
        $user["clientNo"]=$clientNo;
        $user["carPlateNo"]=$carPlateNo;
        $user["reqNo"]=$reqNo;
        $user["fd"]=$fd;
        Db::table("client_login_car")->insert($user);
        //查询客户端登录的信息
        
        //查询客户端登录的信息
        $logininfo=Db::table("client_login")
                ->alias("a")
                ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                ->where("a.clientNo",$indata["clientNo"])
                ->where("a.parkingNo",$indata["parkingNo"])
                ->find();
       
        
        $parkingName=$logininfo["parkingName"];
        $response["parkingNo"]=$parkingNo;
        $response["version"]="V1.0";
        $response["parkingName"]=$parkingName;
        $response["parkingOrderNo"]=$indata["parkingOrderNo"];
        $response["outParkingOrderNo"]=$indata["outParkingOrderNo"];
        $response["clientNo"]=$clientNo;
        $response["bizCode"]="B1015";
        $response["reqNo"]=$reqNo;//$logininfo["reqNo"];//$reqNo;
        $response["orderType"]="prePay";//
        $response["queryType"]="01";
        $parkingKey=$logininfo["parkingKey"];
        //$parkingKey="600006";
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        
        $result["result"]=$this->jsondata($response);
        $result["fd"]=$logininfo["fd"];
        return $result;
    }
    
    //扫码缴费查询 $fd用户标志
    public function outPay($indata,$fd){
        Db::table("client_login_car")->where("fd",$fd)->delete();
        $parkingNo=$indata["parkingNo"];//停车场
        $clientNo=$indata["clientNo"];//客户端
        $cam_id=$indata["cam_id"];//出口相机id
        $reqNo=date("YmdHis")."-".rand(100000,999999);
        
        $user["parkingNo"]=$parkingNo;
        $user["clientNo"]=$clientNo;
        $user["cam_id"]=$cam_id;
        $user["reqNo"]=$reqNo;
        $user["fd"]=$fd;
        Db::table("client_login_car")->insert($user);
        
        //查询客户端登录的信息
        $logininfo=Db::table("client_login")
                ->alias("a")
                ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                ->where("a.clientNo",$indata["clientNo"])
                ->where("a.parkingNo",$indata["parkingNo"])
                ->find();
        $parkingName=$logininfo["parkingName"];
        
        $response["parkingNo"]=$parkingNo;
        $response["version"]="V1.0";
        $response["parkingName"]=$parkingName;
        $response["clientNo"]=$clientNo;
        $response["bizCode"]="B1015";
        $response["reqNo"]=$reqNo;//$logininfo["reqNo"];//$reqNo;
        $response["orderType"]="outPay";//
        $response["outChannelNo"]=$cam_id;
        
        $response["queryType"]="01";
        $parkingKey=$logininfo["parkingKey"];
        //$parkingKey="600006";
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        
        $result["result"]=$this->jsondata($response);
        $result["fd"]=$logininfo["fd"];
        return $result;
    }
    
    //车辆入场
    public function uploadIn($indata){
        $result=(new swooleService())->uploadIn($indata);
        return $result;
        
    }
    //登录操作
    public function login($indata,$fd){
        
        $result=(new swooleService())->login($indata,$fd);
        return $result;
        
       
    }
    //心跳操作
    public function heartbeat($indata){
        $result=(new swooleService())->heartbeat($indata);
        return $result;
    }
    
    private function jsondata($arr){
        $str='{';
        foreach($arr as $key =>$value){
            $str=$str.'"'.$key.'":"'.$value.'",';
        }
        $str = trim($str, ",");
        $str=$str.'}';
        return $str;
    }
    private function MakeSign($arr,$parkingKey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //return $string;
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $parkingKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    
    //格式化参数格式化成url参数(str)
    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= strtolower($k) . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
    
}